<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

if (!defined("RealBet_lib_include")){
	define("RealBet_lib_include", 1);

/**
 * 各階層停押
 * @param	intGID：操盤序號
 * @param	strBType：下注種類序號字串，ex: 1,3,..,n
 * @param	strUserID：使用者序號字串，ex: 1,3,..,n
 * @return	Array[?x][?y][?z] -> 成功，false -> 失敗，?x -> 操盤序號，?y -> 下注種類序號，?z -> 球號
*/
	function GetStopRealBet($intGID, $strBType, $strUserID, $intSefUID = 0){
		global $_Conn, $$_Conn;

		$strSQL = "select UserID, GambleID, Bet_Type, Number from `bet_StopRealBet` where GambleID = ".$intGID." and Bet_Type in (".$strBType.") and UserID in (".$strUserID.") and isStopBet = 1";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Stop Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrStop = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			if($Rows -> UserID != $intSefUID){
				$arrStop["parent"][$Rows -> GambleID][$Rows -> Bet_Type][$Rows -> Number] = 1;
			}else{
				$arrStop["self"][$Rows -> GambleID][$Rows -> Bet_Type][$Rows -> Number] = 1;
			}
		}

		return $arrStop;
	}

/**
 * 單一號碼各階層是否停押
 * @param	intGID：操盤序號
 * @param	intBType：下注種類序號字串，ex: 1,3,..,n
 * @param	intNum：
 * @param	strUserID：使用者序號字串，ex: 1,3,..,n
 * @return	String -> 成功，false -> 失敗
"Stop" -> 停押
*/
	function GetOneStopWagerBet($intGID, $intBType, $intNum, $strUserID){
		global $_Conn, $$_Conn;

		$strSQL = "select * from `bet_StopRealBet` where GambleID = ".$intGID." and Bet_Type = ".$intBType." and UserID in (".$strUserID.") and isStopBet = 1 and Number = ".$intNum;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Stop Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		return (PMA_mysql_num_rows($RS) > 0)?"Stop":"OK";
	}

/**
 * 各階層停押
 * @param	intGID：操盤序號
 * @param	intBType：下注種類序號
 * @param	intStopBet：停押或開放，0 -> 開放，1 -> 停押
 * @param	intNum：號碼種類
 * @param	intUserID：使用者序號
 * @return	true -> 成功，false -> 失敗
*/
	function StopLayerRealBet($intGID, $intBType, $intStopBet, $intNum, $intUserID){
		global $_ConnMaster;

		$strSQL = "update `bet_StopRealBet` set isStopBet = ".$intStopBet.", LastTime = UNIX_TIMESTAMP() where GambleID = ".$intGID." and Bet_Type = ".$intBType." and Number = ".$intNum." and UserID = ".$intUserID;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Stop Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		if(mysql_affected_rows($_ConnMaster) == 0){
			$strSQL = "insert into `bet_StopRealBet`(GambleID, UserID, Bet_Type, Number, isStopBet, LastTime) values(".$intGID.",".$intUserID.",".$intBType.",".$intNum.",".$intStopBet.",UNIX_TIMESTAMP())";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "Add Stop Wager Data Error!!";
				$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
				return false;
			}
		}
		return true;
	}

/**
 * 各階層停押所有遊戲
 * @param	intGID：操盤序號
 * @param	intLID：樂透種類序號
 * @param	intBType：下注種類序號
 * @param	intStopBet：停押或開放，0 -> 開放，1 -> 停押
 * @param	strNum：號碼種類字串，1,4,..,n
 * @param	intUserID：使用者序號
 * @return	true -> 成功，false -> 失敗
*/
	function StopLayerAllRealBet($intGID, $intLID, $_strBType, $intStopBet, $_strNum, $intUserID){
		$arrBetType = split(",", $_strBType);
		while(list($k, $v) = each($arrBetType)){
			switch($v){
				case 1:
				case 4:
					$arrNum = split(",", $_strNum);
					for($i = 0; $i < sizeof($arrNum); $i++)if(!StopLayerRealBet($intGID, $v, $intStopBet, $arrNum[$i], $intUserID))return false;
					break;

				case 2:
				case 3:
				case 5:
				case 6:
					for($i = 0; $i < 2; $i++)if(!StopLayerRealBet($intGID, $v, $intStopBet, $i, $intUserID))return false;
					break;
			}
		}
		return true;
	}


//-------------------------------------------------------//
//RealBet_lib End
}
?>